home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / varia / silo.lha / silo / SimEntityList.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  3KB  |  132 lines

  1. /* $Author: ecsv38 $ $Date: 90/08/21 14:46:03 $ $Revision: 1.1 $ */
  2. /* (c) S. Manoharan  sam@lfcs.edinburgh.ac.uk */
  3.  
  4. #include <stream.h>
  5.  
  6. #include "SimEntityList.h"
  7.  
  8.  
  9. SimEntityList::~SimEntityList()
  10. {
  11.    SimEntityItem *listnode = head;
  12.  
  13.    while ( listnode ) {
  14.       SimEntityItem *temp = listnode;
  15.  
  16.       listnode = listnode->next;
  17.       delete temp;
  18.    }
  19.    head = tail = 0;
  20. }
  21.  
  22. void
  23. SimEntityList::insert(Entity *const entity)
  24. {
  25.    SimEntityItem *listnode = new SimEntityItem(entity);
  26.    listnode->next = head;
  27.    if ( head ) head->prev = listnode;
  28.    else tail = listnode;
  29.    head = listnode;
  30. }
  31.  
  32. void
  33. SimEntityList::append(Entity *const entity)
  34. {
  35.    SimEntityItem *listnode = new SimEntityItem(entity);
  36.    listnode->prev = tail;
  37.    if ( tail ) tail->next = listnode;
  38.    else head = listnode;
  39.    tail = listnode;
  40. }
  41.  
  42. Entity *
  43. SimEntityList::remove(const int eid)
  44. {
  45.    SimEntityItem *listnode = head;
  46.    SimEntityItem *temp;
  47.    Entity * gotcha = 0;
  48.  
  49.    while ( listnode ) {
  50.       if ( (listnode->entity)->id() == eid ) {
  51.      gotcha = listnode->entity;
  52.      temp = listnode;
  53.          if ( listnode->prev != 0 )
  54.             (listnode->prev)->next = listnode->next;
  55.          else head = listnode->next;
  56.          if ( listnode->next != 0 )
  57.             (listnode->next)->prev = listnode->prev;
  58.      else tail = listnode->prev;
  59.          listnode = listnode->next;
  60.          delete temp;
  61.      break;
  62.       }
  63.       else listnode = listnode->next;
  64.    }
  65.    return gotcha;
  66. }
  67.  
  68. int
  69. SimEntityList::max_entity_id()
  70. {
  71.    SimEntityItem *listnode = head;
  72.  
  73.    if ( head == 0 ) return 0;
  74.  
  75.    int max = (head->entity)->priority();
  76.    int eid = (head->entity)->id();
  77.    while ( listnode ) {
  78.       if ( (listnode->entity)->priority() > max ) {
  79.      max = (listnode->entity)->priority();
  80.      eid = (listnode->entity)->id();
  81.       }
  82.       listnode = listnode->next;
  83.    }
  84.    return eid;
  85. }
  86.  
  87. int
  88. SimEntityList::min_entity_id()
  89. {
  90.    SimEntityItem *listnode = head;
  91.  
  92.    if ( head == 0 ) return 0;
  93.  
  94.    int min = (head->entity)->priority();
  95.    int eid = (head->entity)->id();
  96.    while ( listnode ) {
  97.       if ( (listnode->entity)->priority() < min ) {
  98.      min = (listnode->entity)->priority();
  99.      eid = (listnode->entity)->id();
  100.       }
  101.       listnode = listnode->next;
  102.    }
  103.    return eid;
  104. }
  105.  
  106. void
  107. SimEntityList::print()
  108. {
  109.    if ( listname != 0 ) cout << listname << ":\n";
  110.  
  111.    if ( head == 0 ) {
  112.       cout << "[ empty ]\n";
  113.       return;
  114.    }
  115.  
  116.    cout << "[ ";
  117.  
  118.    SimEntityItem *listnode = head;
  119.    const int lineLength = 16;
  120.    int cnt = 0;
  121.    while ( listnode != 0 ) {
  122.       if ( ++cnt % lineLength == 1 && cnt != 1 )
  123.      cout << "\n  ";
  124.       /* print list->entry at this point */
  125.       cout << form("0x%x (%g) ",
  126.      (listnode->entity)->id(), (listnode->entity)->priority());
  127.       listnode = listnode->next;
  128.    }
  129.    cout << "]\n";
  130. }
  131.  
  132.